﻿using GFE.Interfaces.Sample.Decorators;
using GFE.SharePoint.Interfaces;
using GFE.SharePoint.CSharpExtensions;
using GFE.Webpart.Sample.Views.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GFE.Webpart.Sample.ViewModels;
using GFE.Interfaces.Sample.Models;

namespace GFE.Webpart.Sample.Presenters
{
    public class CustomerListPresenter : IGFEMVPPresenter<ICustomerListView>
    {
    
        public ICustomerListView View {get; set;}

        public CustomerListPresenter(ICustomerListView View)
        {
            this.View = View;
            AttachViewEvents();
        }

        void AttachViewEvents() {
            View.OnLoadModel += View_OnLoadModel;
            View.OnGetCustomerList += View_OnGetCustomerList;
        }

        void View_OnGetCustomerList(object sender, EventArgs e)
        {
            var customerServiceDecorator = new CustomerServiceDecorator();

            View.Model.CustomerList = customerServiceDecorator.GetCustomerList().MapListTo<CustomerModel, CustomerListViewModelItem>((item) =>
            {
                return new CustomerListViewModelItem()
                {
                    Name = item.Name,
                    Address = item.Address
                };
            });
        }

        void View_OnLoadModel(object sender, EventArgs e)
        {
            View.Model = View.Model ?? new CustomerListViewModel();
        }


    }
}
